//
//  ViewController.m
//  searchbar
//
//  Created by 组织者 on 16/11/22.
//  Copyright © 2016年 xmk. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UISearchBarDelegate,UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *web;

@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *actv;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UISearchBar *search = [UISearchBar new];
    search.returnKeyType = UIReturnKeySearch;
    search.keyboardType = UIKeyboardTypeURL;
    NSLog(@"宽：%f,高：%f",[UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height);
    UIToolbar *tool =[[UIToolbar alloc] initWithFrame:CGRectMake(0, 524, [UIScreen mainScreen].bounds.size.width, 44)];
    tool.backgroundColor = [UIColor colorWithRed:77/255.0 green:152/255.0 blue:258/255.0 alpha:1.0];
    
    UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:@"<" style:UIBarButtonItemStylePlain target:self action:@selector(onBack:)];
    [back setTintColor:[UIColor whiteColor]];
    
    UIBarButtonItem *goware = [[UIBarButtonItem alloc] initWithTitle:@">" style:UIBarButtonItemStylePlain target:self action:@selector(onGoware:)];
    [goware setTintColor:[UIColor whiteColor]];
    
    UIBarButtonItem *frash = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(onFrash:)];
    
    tool.items = @[space,back,goware,frash];
    [self.view addSubview:tool];
    
    
    self.navigationItem.titleView = search;
    self.web.delegate = self;
    search.delegate = self;
}

- (void)onFrash:(UIBarButtonItem *)sender
{
    [self.web reload];
    [self.actv startAnimating];
}

- (void)onBack:(UIBarButtonItem *)sender
{
    if([self.web canGoBack])
    {
        [sender setTintColor:[UIColor whiteColor]];
        [self.web goBack];
    }
    
    else
    {
        [sender setTintColor:[UIColor whiteColor]];
        NSLog(@"1");
    }
}

- (void)onGoware:(UIBarButtonItem *)sender
{
    if([self.web canGoForward])
    {
        [sender setTintColor:[UIColor yellowColor]];
        [self.web goForward];
    }
    else
    {
        [sender setTintColor:[UIColor grayColor]];
        NSLog(@"2");
    }

}


- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    NSString *str = searchBar.text;
    //NSLog(@"1111111111111");
    if(![str hasPrefix:@"http://"]){
        str = [@"http://" stringByAppendingString:str];
    }
      NSURL *url = [NSURL URLWithString:str];
    NSURLRequest *req = [NSURLRequest requestWithURL:url];
    [self.web loadRequest:req];
    [searchBar resignFirstResponder];
    [self.actv startAnimating];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    [self.actv stopAnimating];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//zfhnb
@end
